package com.zxh.okretrofit;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.zxh.okretrofit.entity.BaseEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zhangxuehui on 2018/2/8.
 */

public class CallbackImpl<T> implements Callback<BaseEntity<T>> {
    private Listener<T> listener;
    private Context context;
    private boolean isShowTip;//false显示错误提示，true不显示

    CallbackImpl(Context context, Listener<T> listener) {
        this.context = context;
        this.listener = listener;
    }

    CallbackImpl(Context context, Listener<T> listener, boolean isShowTip) {
        this.context = context;
        this.listener = listener;
        this.isShowTip = isShowTip;
    }

    @Override
    public void onResponse(Call<ServerResult<T>> call, Response<ServerResult<T>> response) {
        try {
            ServerResult<T> serverResult = response.body();
            int errorCode = serverResult.getErrorCode();
            boolean success = serverResult.isSuccess();
            String msg = serverResult.getMsg();

//                LogUtils.e("CrashService--------code:" + errorCode + "   msg:" + msg + " data:" + success + "--------");
            if (errorCode == NetWorkStatus.TOKEN_ERROR) {
                showTokenDialog(context);
                SpUtils.clearToken(context);
            } else if (errorCode == NetWorkStatus.NOT_SUFFICIENT) {
                //for createOrder()
                Intent intent = new Intent(context, NotSufficientDialog.class);
                intent.putExtra(Utility.CommenConfirmDialogDisplayContent, msg);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                context.startActivity(intent);
            } else if (NetWorkStatus.IF_NEED_CONFIRM_CODE == errorCode) {
                //拦截9999错误码，以便下面的条件正常使用
            } else if (errorCode != NetWorkStatus.SUCCESS && !TextUtils.isEmpty(msg) && !isShowTip) {
//                    showErrorDialog(context, msg);
                ToastUtils.show(context, msg);
            }
            if (errorCode == 0 && !success) {
                errorCode = NetWorkStatus.RESPONSE_RESULT_ERROR;
            }
//                if (errorCode == NetWorkStatus.ERROR) {
//                    ToastUtils.show(context, R.string.network_conn_error, Toast.LENGTH_SHORT);
//                    return;
//                }
            if (listener != null) {
                listener.onFinish(errorCode, msg, serverResult.getData());
            }
        } catch (Exception e) {
            e.printStackTrace();
//                if (HttpOnNextListener != null) {
            LogUtils.e("请求异常" + e.getMessage());
//                ToastUtils.show(context, R.string.network_conn_error, Toast.LENGTH_SHORT);
//                return;
            listener.onFinish(NetWorkStatus.ERROR, null, null);
//                }
        }
    }

    @Override
    public void onFailure(Call<ServerResult<T>> call, Throwable t) {
        t.printStackTrace();
        if (listener != null) {
            listener.onFinish(NetWorkStatus.ERROR, null, null);
        }
    }

}